package org.firecloud.h5fun.controller.servlet;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.firecloud.h5fun.model.CommonConfig;
import org.firecloud.h5fun.model.CommonQuery;
import org.firecloud.h5fun.model.javabean.ApplicationBean;
import org.firecloud.h5fun.model.javabean.ProjectBean;

public class ShowApplication extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Connection conn = null;
    	try {
    		int appId = Integer.parseInt(req.getParameter("id"));
    		CommonConfig config = (CommonConfig) req.getServletContext().getAttribute("commonConfig");
    		conn = config.getConn();
    		ApplicationBean application = CommonQuery.select_application(conn, appId);
			ProjectBean project = CommonQuery.select_project(conn,application.getProjectId());
			CommonQuery.update_app_inc_click(conn, appId);
			req.setAttribute("project",project);
			req.setAttribute("application",application);
			req.setAttribute("IframeUrl",String.format("files/apps/%s/index.html?id=%d", project.getIframeUrl(),appId));
			conn.close();
			req.getRequestDispatcher("jsp/page-show-application.jsp").forward(req, resp);
		} catch (Exception e) {
			Logger.getRootLogger().error(e.toString());
			req.getRequestDispatcher("jsp/page-error.jsp").forward(req, resp);
		}finally {
			if(conn!=null){
				try {
					conn.close();
				} catch (Exception e) {
					Logger.getRootLogger().equals(e);
				}
			}
		}
	}
}
